/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MissionButton.java 566 2010-07-25 20:55:33Z Harry $
 *   $LastChangedDate: 2010-07-25 20:55:33 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.resource.Graphics;
import battlefield.resource.Sounds;
import battlefield.entity.Mission;
import battlefield.util.ImageUtils;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 *
 * @author xiankun
 */
public class MissionButton extends JButton implements MouseListener {

    public static final int ICON_WIDTH = 100;
    public static final int ICON_HEIGHT = 100;
    private Image backgroundImage;
    private Mission mission;

    public MissionButton(Mission mission) {
        this.mission = mission;
        initComponents();
    }

    public Mission getMission() {
        return mission;
    }

    public void setMission(Mission mission) {
        this.mission = mission;
    }

    public void initComponents() {
        this.setFocusPainted(false);
        this.setBorderPainted(false);
        this.setContentAreaFilled(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setOpaque(false);
        this.setIcon(new ImageIcon(ImageUtils.resize(Graphics.get(mission.getImageSrc()).getBufferedImage(), 75, 75)));
        this.setText("<html><center>" + mission.getMissionName() + "<BR /><font color='red'>Gold:" + mission.getGoldReceive() + "</font><BR />Job Complete: " + mission.getMissionComplete() + "%</center></html>");
        this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        this.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        if (mission.isEnable()) {
            this.backgroundImage = Graphics.BUTTON_MISSION_NORMAL_SELECTED.getImage();
        } else {
            this.backgroundImage = Graphics.BUTTON_MISSION_NORMAL.getImage();
        }
        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(java.awt.Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.isSelected() ? 1f : 0.6f));
        g2d.drawImage(backgroundImage, 0, 0, this);
        super.paintComponent(g2d);
        g2d.dispose();
    }

    public void mouseClicked(MouseEvent e) {
        this.repaint();
    }

    public void mousePressed(MouseEvent e) {
        Sounds.MOUSE_CLICK.play();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        if (mission.isEnable()) {
            this.backgroundImage = Graphics.BUTTON_MISSION_HOVER_SELECTED.getImage();
        } else {
            this.backgroundImage = Graphics.BUTTON_MISSION_HOVER.getImage();

        }
        this.repaint();
        Sounds.MOUSE_HOVER.play();

    }

    public void mouseExited(MouseEvent e) {
        if (mission.isEnable()) {
            this.backgroundImage = Graphics.BUTTON_MISSION_NORMAL_SELECTED.getImage();
        } else {
            this.backgroundImage = Graphics.BUTTON_MISSION_NORMAL.getImage();
        }

        this.repaint();
    }
}
